<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="名称" prop="businessName">
        <el-input
          v-model="queryParams.businessName"
          placeholder="请输入名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="地址" prop="address">
        <el-input
          v-model="queryParams.address"
          placeholder="请输入地址"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item label="审核状态" prop="state">
        <el-select v-model="queryParams.state" placeholder="请选择审核状态" clearable :style="{width: '100%'}">
          <el-option v-for="(item, index) in stateOptions" :key="index" :label="item.label"
                     :value="item.value" :disabled="item.disabled"></el-option>
        </el-select>
      </el-form-item>


      <el-form-item label="电话号码" prop="tel">
        <el-input
          v-model="queryParams.tel"
          placeholder="请输入电话号码"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['member:member:add']"
        >新增
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['member:member:edit']"
        >修改
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['member:member:remove']"
        >删除
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['member:member:export']"
        >导出
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="memberList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center"/>
      <el-table-column label="" align="center" prop="id"/>
      <el-table-column label="商家名称" align="center" prop="businessName"/>
      <el-table-column label="商家地址" align="center" prop="address"/>
      <el-table-column label="店铺图片" align="center" prop="backImg">
        <template slot-scope="scope">
          <el-carousel height="70px" width="70px" indicator-position="none" >
            <el-carousel-item v-for="item in splitImg(scope.row.backImg)" :key="item">
              <el-image
                style="width: 70px; height: 70px"
                :src="item"
              ></el-image>

            </el-carousel-item>
          </el-carousel>
        </template>
      </el-table-column>
      <el-table-column label="地址坐标" align="center" prop="mapCode"/>
      <el-table-column label="审核状态" align="center" prop="state">
       <template slot-scope="scope">
         <dict-tag :options="dict.type.member_status" :value="scope.row.state"/>
       </template>
      </el-table-column>
      <el-table-column label="是否营业" align="center" prop="workState">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.member_work_state" :value="scope.row.workState"/>
        </template>
      </el-table-column>
      <el-table-column label="电话号码" align="center" prop="tel"/>
      <el-table-column label="店铺类型" align="center" prop="shopType"/>
      <el-table-column label="创建时间" align="center" prop="addTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.addTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" fixed="right">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['member:member:edit']"
          >修改
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['member:member:remove']"
          >删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改#商家信息对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="60%" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">

        <el-row>
          <el-col :span="12">
            <el-form-item label="商家名称" prop="businessName" required>
              <el-input v-model="form.businessName" placeholder="请输入商家名称"/>
            </el-form-item>
          </el-col>

          <el-col :span="12">
            <el-form-item label="店铺类型" prop="shopType" required>
              <el-input v-model="form.shopType" placeholder="请输入店铺类型"/>
            </el-form-item>
          </el-col>
        </el-row>


        <el-row>
          <el-col :span="12">
            <el-form-item label="配送金额" prop="deliveryFee">
              <el-input v-model="form.deliveryFee" placeholder="请输入配送金额" oninput="value=value.replace(/[^\d]/g,'')">
                <template slot="append">元</template>
              </el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="配送距离" prop="deliveryRadius">
              <el-input v-model="form.deliveryRadius" placeholder="请输入配送距离" oninput="value=value.replace(/[^\d]/g,'')">
                <template slot="append">km</template>
              </el-input>
            </el-form-item>
          </el-col>
        </el-row>


        <el-form-item label="店铺图片" prop="backImg">
          <uploadImage v-model="form.backImg"
          >
          </uploadImage>
        </el-form-item>

        <div>
          <baiduMap
            @map-baidu="getjw"
          >
          </baiduMap>
        </div>

        <el-row>
          <el-col :span="12">
            <el-form-item label="商家地址" prop="address">
              <el-input v-model="form.address" placeholder="请输入商家地址" :disabled="true"/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="经纬度" prop="mapCode">
              <el-input
                v-model="form.mapCode"
                placeholder="请输入经纬度坐标"
                :disabled="true"
              />
            </el-form-item>
          </el-col>
        </el-row>


        <el-row>
          <el-col :span="12">
            <el-form-item label="审核状态" prop="state">
              <el-radio-group v-model="form.state">
                <el-radio
                  v-for="dict in stateOptions"
                  :key="dict.value"
                  :label="dict.value"
                >{{ dict.label }}
                </el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="营业状态" prop="workState">
              <el-radio-group v-model="form.workState">
                <el-radio
                  v-for="dict in workStateOptions"
                  :key="dict.value"
                  :label="dict.value"
                >{{ dict.label }}
                </el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col>
        </el-row>


        <el-row>
          <el-col :span="12">
            <el-form-item label="电话号码" prop="tel" >
              <el-input v-model="form.tel" placeholder="请输入电话号码" oninput="value=value.replace(/[^\d]/g,'')">
              </el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="登陆密码" prop="password">
              <el-input type="password" v-model="form.password" show-password placeholder="请输入登陆密码" oninput="value=value.replace(/[^\d]/g,'')">
              </el-input>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="12">
            <el-form-item label="老板姓名" prop="trueName">
              <el-input v-model="form.trueName" placeholder="请输入老板姓名"/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="账户余额" prop="amount">
              <el-input v-model="form.amount" placeholder="请输入账户余额" oninput="value=value.replace(/[^\d]/g,'')">
              </el-input>
            </el-form-item>
          </el-col>
        </el-row>

      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {addMember, delMember, getMember, listMember, updateMember} from "@/api/member/member";
import fr from "element-ui/src/locale/lang/fr";
import baiduMap from "@/components/baiduMap";
import Vue from 'vue'
import BaiduMap from 'vue-baidu-map'
import uploadImage from "@/components/ImageUpload/index"
Vue.use(BaiduMap, {
  ak: 'Phqhd1m9VR4giYknwRswR3jRln9qpuSq'
})

export default {
  name: "Member",
  components: {baiduMap, uploadImage},
  dicts: ['member_status','member_work_state'],
  computed: {
    fr() {
      return fr
    },
  },
  data() {
    return {
      showMapComponent: true,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // #商家信息表格数据
      memberList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 删除标记 0.未删除 1.已删除时间范围
      daterangeAddTime: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        businessName: null,
        deliveryFee: null,
        deliveryRadius: null,
        address: null,
        backImg: null,
        mapCode: null,
        state: null,
        workState: null,
        tel: null,
        password: null,
        trueName: null,
        shopType: null,
        token: null,
        amount: null,
        addTime: null,
        isDeleted: null
      },
      stateOptions: [{
        "label": "已审核",
        "value": 1
      }, {
        "label": "未审核",
        "value": 0
      }],
      workStateOptions: [{
        "label": "正常营业",
        "value": 1
      }, {
        "label": "歇业",
        "value": 0
      }],
      // 表单参数
      form: {},
      // 表单校验
      rules: {}
    };
  },
  created() {
    this.getList();
  },
  mounted() {
  },
  methods: {
    // // 文件提交处理
    // submitUpload() {
    //   this.$refs.upload.submit();
    // },
    // // 文件上传中处理
    // handleFileUploadProgress(event, file, fileList) {
    //   this.upload.isUploading = true;
    // },
    // // 文件上传成功处理
    // handleFileSuccess(response, file, fileList) {
    //   this.upload.isUploading = false;
    //   this.form.filePath = response.url;
    //   this.msgSuccess(response.msg);
    // },

    //切割url
    splitImg(url) {
      return url.split(",")
    },
    /** 获取经纬度 */
    getjw(center) {
      // console.log(center);
      this.form.address = center.searchJingwei;
      this.form.mapCode = center.longitude + ',' + center.latitude;
      // console.log(this.from.mapCode)
    },

    /** 查询商家信息列表 */
    getList() {
      this.loading = true;
      this.queryParams.params = {};
      if (null != this.daterangeAddTime && '' != this.daterangeAddTime) {
        this.queryParams.params["beginAddTime"] = this.daterangeAddTime[0];
        this.queryParams.params["endAddTime"] = this.daterangeAddTime[1];
      }
      listMember(this.queryParams).then(response => {
        this.memberList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        businessName: null,
        deliveryFee: null,
        deliveryRadius: null,
        address: null,
        backImg: null,
        mapCode: null,
        state: null,
        workState: null,
        tel: null,
        password: null,
        trueName: null,
        shopType: null,
        token: null,
        amount: null,
        addTime: null,
        isDeleted: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.daterangeAddTime = [];
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加商家信息";
      // this.upload.fileList = [];
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getMember(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改商家信息";
      });
      // this.upload.fileList = [{name: this.form.fileName, url: this.form.filepath}];
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateMember(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addMember(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除#商家信息编号为"' + ids + '"的数据项？').then(function () {
        return delMember(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('member/member/export', {
        ...this.queryParams
      }, `member_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
